package cn.liuxue.lifecircle.camera;

import java.io.IOException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/** A basic Camera preview class */

@SuppressLint("NewApi")
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    private String TAG = "CameraPreview";

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        Parameters paras = mCamera.getParameters();
        Log.d(TAG, paras.getAntibanding());
        Log.d(TAG, "==getColorEffect==" + paras.getColorEffect());
        Log.d(TAG, "==getFlashMode==" + paras.getFlashMode());
        Log.d(TAG, "==getFocusMode==" + paras.getFocusMode());
        Log.d(TAG, "==getSceneMode==" + paras.getSceneMode());
        Log.d(TAG, "==getWhiteBalance==" + paras.getWhiteBalance());
        Log.d(TAG, "==getExposureCompensation==" + paras.getExposureCompensation());
        Log.d(TAG, "==getExposureCompensationStep==" + paras.getExposureCompensationStep());
        Log.d(TAG, "==getFocalLength==" + paras.getFocalLength());
        
        Log.d(TAG, "==getHorizontalViewAngle==" + paras.getHorizontalViewAngle());
        Log.d(TAG, "==getJpegQuality==" + paras.getJpegQuality());
        Log.d(TAG, "==getJpegThumbnailQuality==" + paras.getJpegThumbnailQuality());
        Log.d(TAG, "==getJpegThumbnailSize==" + paras.getJpegThumbnailSize());
        Log.d(TAG, "==getMaxExposureCompensation==" + paras.getMaxExposureCompensation());
        Log.d(TAG, "==getMaxZoom==" + paras.getMaxZoom());
        Log.d(TAG, "==getPictureFormat==" + paras.getPictureFormat());
        Log.d(TAG, "==getPictureSize==" + paras.getPictureSize());
        
        paras.setRotation(DRAWING_CACHE_QUALITY_AUTO);
        

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}